package ru.p03.tvipuskeditor;

import java.util.HashMap;
import java.util.Map;

import ru.p03.tvipuskeditor.client.tvipuskeditor.TvipuskEditorClientRpc;
import ru.p03.tvipuskeditor.client.tvipuskeditor.TvipuskEditorServerRpc;
import ru.p03.tvipuskeditor.client.tvipuskeditor.TvipuskEditorState;

import com.vaadin.shared.MouseEventDetails;

public class TvipuskEditor extends com.vaadin.ui.AbstractField {
	
	public static final String REQUEST_FILENAME = "client_request_filename";

	public static final String REQUEST_FILENAME_VAL = "client request filename";

	public static final String RETURN_FILENAME = "client_return_filename";

	public static final String RETURN_TEXT = "return_text";

	private TvipuskEditorServerRpc rpc = new TvipuskEditorServerRpc() {
		//private int clickCount = 0;

		public void clicked(MouseEventDetails mouseDetails) {
//			// nag every 5:th click using RPC
//			
//			if (++clickCount % 5 == 0) {
//				getRpcProxy(TvipuskEditorClientRpc.class).alert(
//						"Ok, that's enough!");
//			}
//			
//			// update shared state
//			getState().text = "You have clicked " + clickCount + " times";
		}
		
//		public void doSave() {
//			// nag every 5:th click using RPC
//
//				getRpcProxy(TvipuskEditorClientRpc.class).alert(
//						"Ok, that's enough!");
//			
//			// update shared state
//			
//			getState().text = "You have clicked " + clickCount + " times";
//		}


//		@Override
//		public void save() {
//			
//			System.out.println("save was called");
//			System.out.println("text is: " + getState().text);
//			
//			
//		}

//		@Override
//		public void setText(String a_text) {
//			// TODO Auto-generated method stub
//			text = a_text ;
//			System.out.println("setText was called, text is:" + text);
//		}
		
		@Override
		public void fireGetTextEventRpc(String text) {
			Map<String,String> valMap = new HashMap<String,String>();
			valMap.put(RETURN_TEXT, text);
			setValue(valMap);
		}

	};  

	public TvipuskEditor() {
		registerRpc(rpc);
	}

//	//@Override
//	public TvipuskEditorState getState() {
//		return (TvipuskEditorState) super.getState();
//	}	

	public void sendGetTextRequest(){
		getRpcProxy(TvipuskEditorClientRpc.class).sendGetTextRequest();
	}

	public void sendEnabled(boolean enabled){
		getRpcProxy(TvipuskEditorClientRpc.class).setEndabled(enabled);
	}	
	
	public void sendFilename(String filename){
		getRpcProxy(TvipuskEditorClientRpc.class).sendFilename(filename);
	}

	public void sendDuration(String duration){
		getRpcProxy(TvipuskEditorClientRpc.class).sendDuration(duration);
	}
	
	public void setTeWidth(String width) {
		getRpcProxy(TvipuskEditorClientRpc.class).setTeWidth(width);
	}
	
	public void setTeHeight(String height) {
		getRpcProxy(TvipuskEditorClientRpc.class).setTeHeight(height);
	}
	
	
	public void setXmlText(String xmlText) {
		getRpcProxy(TvipuskEditorClientRpc.class).setXmlText(xmlText);
	}
	
	
	@Override
	public Class<?> getType() {
		return String.class;
	}



}
